package cn.mania_man.trpg_helper_server.room;

import cn.mania_man.trpg_helper_server.util.ResTypeEnum;
import cn.mania_man.trpg_helper_server.util.UserTagEnum;
import cn.mania_man.trpg_helper_server.websocket.User;
import com.alibaba.fastjson.JSONObject;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class ResponseMessage {
    private String body;
    private Set<String> senderRole = new HashSet<>();
    private Set<User> receivers = new HashSet<>();
    private String msgId;

    private ResTypeEnum type;

    public ResponseMessage(){

    }
    public ResponseMessage(String body,User sender,ResTypeEnum type){
        this.setBody(body);
        this.setSenderRole(sender.getRole());
        this.setType(type);
    }
    public void send() {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("body", this.body);
        jsonObject.put("sender", this.senderRole);
        jsonObject.put("receiver", this.receivers.stream().map(User::getUserName).collect(Collectors.toList()));
        jsonObject.put("type", this.type);
        jsonObject.put("msgId", this.getMsgId());
        String msg = jsonObject.toJSONString();
        receivers = receivers.stream().filter(user -> user.getSession().isOpen()).collect(Collectors.toSet());
        receivers.forEach(user -> user.sendMessage(msg));
    }
    public void ignoreUser(User ignoreUser){
        receivers.remove(ignoreUser);
    }
    public void addReceiver(String receiver, Room room,boolean all) {
        if ("*".equals(receiver)) {
            this.receivers.addAll(room.getUsers());
        } else {
            int firstSplitChar = receiver.indexOf(":");
            String type = receiver.substring(0, firstSplitChar);
            String value = receiver.substring(firstSplitChar + 1);
            switch (type) {
                case "role": {
                    room.getUsers().stream().filter(user -> user.getRole().contains(value))
                            .forEach(this.receivers::add);
                    break;
                }
                case "name": {
                    room.getUsers().stream().filter(user -> user.getUserName().equals(value))
                            .forEach(this.receivers::add);
                    break;
                }
                case "tag": {
                    room.getUsers().stream().filter(user -> user.getTag().contains(UserTagEnum.valueOf(value)))
                            .forEach(this.receivers::add);
                    break;
                }
            }
        }

    }

    public void pushReceiver(User receiver) {
        this.receivers.add(receiver);
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Set<String> getSenderRole() {
        return senderRole;
    }

    public void setSenderRole(Set<String> senderRole) {
        this.senderRole = senderRole;
    }

    public void addSender(String sender) {
        this.senderRole.add(sender);
    }

    public Set<User> getReceivers() {
        return receivers;
    }


    public ResTypeEnum getType() {
        return type;
    }

    public void setType(ResTypeEnum type) {
        this.type = type;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }

    public String getMsgId() {
        return msgId;
    }
}
